<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书列表展示</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/list.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>

</head>

<body>
    <div class="bookContainer">
        <h2>图书列表展示</h2>
        <div class="navbar-justify-between">
            <div>
                <button class="btn btn-outline-info" type="button" onclick="location.href='book_add.html'">添加图书</button>
                <button class="btn btn-outline-info" type="button" onclick="batchDelete()">批量删除</button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <td>选择</td>
                    <td class="width100">图书ID</td>
                    <td>书名</td>
                    <td>作者</td>
                    <td>数量</td>
                    <td>定价</td>
                    <td>出版社</td>
                    <td>状态</td>
                    <td class="width200">操作</td>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>

        <div class="demo">
            <ul id="pageContainer" class="pagination justify-content-center"></ul>
        </div>
        <script>
            //调用函数，发起ajax请求
            getBookList();
            function getBookList() {
                $.ajax({
                    // 这里即使location.search = null; 到后端就是这样一个对象PageRequest pageRequest = null,
                    // 但是这个对象有默认值，就完全不慌了，真实绝了！！
                    url: "/book/getBookListByPage" + location.search,
                    type: "get",
                    success: function (result) {
                        // 很多判断情况，功能的实现了解即可
                        // console.log(result);
                        // if (result.code == "NOLOGIN") {
                        //     location.href = "login.html";
                        // } else if (result.code == "FAIL") {
                        //     alert("内部错误");
                        //     return;
                        // } else{

                        // }
                        // if (result.data == null || result.data.records == null) {
                        //     alert("没有图书");
                        //     return;
                        // }
                        // if (result.records == null) {
                        //     alert("没有图书")
                        //     return;
                        // }


                        if (result.data != null && result.data.records != null) {
                            var finnalHtml = "";
                            // 我们返回的是一个对象包含book的信息和count，所以遍历的应该是book
                            for (var book of result.data.records) {
                                //用单引号拼接避免转义
                                finnalHtml += '<tr>';
                                finnalHtml += '<td><input type="checkbox" name="selectBook" value="' + book.id + '" id=" selectBook" class="book-select"></td>';
                                finnalHtml += '<td>' + book.id + '</td>';
                                finnalHtml += '<td>' + book.bookName + '</td>';
                                finnalHtml += '<td>' + book.author + '</td>';
                                finnalHtml += '<td>' + book.num + '</td>';
                                finnalHtml += '<td>' + book.price + '</td>';
                                finnalHtml += '<td>' + book.publishName + '</td>';
                                finnalHtml += '<td>' + book.statusCN + '</td>';
                                finnalHtml += '<td>';
                                finnalHtml += '<div class="op">';
                                finnalHtml += '<a href="book_update.html?bookId=' + book.id + '">修改</a>';
                                finnalHtml += '<a href="javascript:void(0)" onclick="deleteBook(' + book.id + ')">删除</a>';
                                finnalHtml += '</div>';
                                finnalHtml += '</td>';
                                finnalHtml += '</tr>';
                            }
                            $("tbody").html(finnalHtml);

                            var data = result.data;
                            //翻页信息,加入分页组件
                            $("#pageContainer").jqPaginator({
                                totalCounts: data.count, //总记录数
                                pageSize: 10,    //每页的个数
                                visiblePages: 5, //可视页数
                                currentPage: data.pageRequest.pageNum,  //当前页码
                                first: '<li class="page-item"><a class="page-link">首页</a></li>',
                                prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页<\/a><\/li>',
                                next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页<\/a><\/li>',
                                last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页<\/a><\/li>',
                                page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}<\/a><\/li>',
                                //页面初始化和页码点击时都会执行
                                onPageChange: function (page, type) {
                                    console.log("第" + page + "页, 类型:" + type);
                                    if (type == "change") {
                                        location.href = "book_list.html?pageNum=" + page;
                                    }

                                }
                            });
                        }




                        
                    },
                    error: function (error) {
                        console.log(error);
                        if (error != null && error.status == 401) {
                            location.href = "login.html";
                        }
                    }

                });
            }


            function deleteBook(id) {
                var isDelete = confirm("确认删除?");//弹出确定删除的框true Or false
                if (isDelete) {
                    //删除图书
                    $.ajax({
                        url: "/book/deleteBook",
                        type: "post",
                        data: {
                            bookId: id//给后端参数bookId赋值id
                        },
                        success: function (result) {
                            if (result.code == "SUCCESS" && result.data == "") {
                                alert("删除成功");
                                location.href = "book_list.html"; //+ location.search;如果当前页面只剩一条数据删掉了就跳转到空页面了
                            } else {
                                console.log(result.code);
                                console.log(result.data);

                                alert("删除失败，请联系管理员");
                            }
                        },
                        error: function (error) {
                            //用户未登录
                            if (error != null && error.status == 401) {
                                location.href = "login.html";
                            }
                        }
                    });
                }
            }
            function batchDelete() {
                var isDelete = confirm("确认批量删除?");//确认框
                if (isDelete) {
                    var ids = [];
                    //input:checkbox: 选择所有的 <input> 元素，其中 type="checkbox"，即选择所有复选框。
                    // [name='selectBook']: 限定选择的复选框是 name 属性为 'selectBook' 的复选框。
                    // :checked: 只选择那些当前已被选中的复选框。
                    $("input:checkbox[name='selectBook']:checked").each(function () {
                        ids.push($(this).val());//当前复选框中的值放到ids中
                    });
                    console.log(ids);
                    $.ajax({
                        url: "/book/batchDeleteBook?ids=" + ids,
                        type: "post",
                        success: function (result) {
                            if (result.code == "SUCCESS" && result.data == "") {
                                alert("删除成功");
                                location.href = "book_list.html";
                            } else {
                                alert("删除失败，请联系管理员");
                            }
                        },
                        error: function (error) {
                            //用户未登录
                            if (error != null && error.status == 401) {
                                location.href = "login.html";
                            }
                        }
                    });
                    // alert("批量删除成功");
                }
            }

        </script>
    </div>
</body>

</html>